package com.ruoyi.modes.controller;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.modes.entity.*;
import com.ruoyi.modes.service.financeInstallmentService;
import com.ruoyi.modes.utis.MyJinRongUtils;
import com.ruoyi.system.api.RemoteFileService;
import com.ruoyi.system.api.domain.SysFile;
import org.apache.poi.hpsf.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @return 申请租房分期
 */
@RestController
@RequestMapping("financeInstallment")
public class financeInstallment {
    @Autowired
    private financeInstallmentService service;

    /**
     * 租房合同查询
     * @return
     */
    @GetMapping("listzf")
    public Object listzf(TJrquery tJrquery){
        PageHelper.startPage(tJrquery.getPageNum(),tJrquery.getPageSize());
        List<CfContractsEntity> listzf = service.listzf();
        return new PageInfo<>(listzf);
    }

    /**
     * 改变下拉框数据
     */
    @GetMapping("listprodect")
    public TJrProduct listprodect(String id){
        System.out.println("下拉框数据++++++++++++++"+id);
        TJrProduct listprodect = service.listprodect(id);
        return listprodect;
    }
    /**
     * 金融产品下拉框数据
     */
    @GetMapping("list")
    public List<TJrProduct> list(){
        List<TJrProduct> list = service.list();
        return list;
    }

    /**
     * 分期列表
     * @return
     */
    @GetMapping("fenqi")
    public HashMap<String,Object> fenqi(Counts counts){
//        System.out.println("前端传值"+counts);
        //授信额度
        BigDecimal sxed = new BigDecimal(counts.getSxed().toString());
        BigDecimal divide = sxed.divide(new BigDecimal("100"), 2, BigDecimal.ROUND_HALF_UP);
        //利率
        BigDecimal nll = new BigDecimal(counts.getNll().toString());
        BigDecimal nll2 = nll.divide(new BigDecimal("100"), 2, BigDecimal.ROUND_HALF_UP);
        //年一次性服务费率
        BigDecimal bigDecimal = new BigDecimal(counts.getFwf().toString());
        BigDecimal fwfl = bigDecimal.divide(new BigDecimal("100"), 2, BigDecimal.ROUND_HALF_UP);


        //借款金额
        BigDecimal jqje = new BigDecimal(counts.getJqje().toString());
        //应付租约合计
        BigDecimal re_lease_count = jqje.multiply(divide);
        //每月还款金额
        BigDecimal yuehk = re_lease_count.divide(new BigDecimal("12"), 2, BigDecimal.ROUND_HALF_UP);
        //分期期限,根据一段时间区间，按月份拆分成多个时间段
        List<MyJinRongUtils.KeyValueForDate> date = MyJinRongUtils.getSplitMonthForDate(DateUtil.parse(counts.getStart_date()),DateUtil.parse(counts.getEnd_date()));
        //总利息
        BigDecimal multiply = jqje.multiply(nll2);
        //月利息(总利息/分期月份)
        BigDecimal yuell = multiply.divide(new BigDecimal("12"), 2, BigDecimal.ROUND_HALF_UP);
        //服务费总金额
        BigDecimal fwf = jqje.multiply(fwfl);
        //还款总额合计
        BigDecimal zong = re_lease_count.add(multiply.add(fwf));
        List<SearchSfLoanDetailMiniResponseDto> spList = new ArrayList<SearchSfLoanDetailMiniResponseDto>();
        int no=1;
        // 循环计算分期详情列表
        for (MyJinRongUtils.KeyValueForDate v : date) {
            SearchSfLoanDetailMiniResponseDto sfdmini = new SearchSfLoanDetailMiniResponseDto();
            sfdmini.setNo(no);
            // 房产编号
            sfdmini.setRoomCode(counts.getCf_contract_no());
            // 房产信息 = 小区名+房产信息
            sfdmini.setRealEstateInfo(counts.getRoom_name());
            // 月利息
            sfdmini.setMonthRateAmount(Double.parseDouble(yuell.toString()));
            // 月租金
            sfdmini.setMonthRepaymentAmount(Double.parseDouble(yuehk.toString()));
            // 合同开始日
            sfdmini.setStarLoanday(v.getStartDate());
            // 合同结束日
            sfdmini.setEndLoanday(v.getEndDate());
            spList.add(sfdmini);
            no++;
        }

        Counts counts1 = new Counts();
        counts1.setRe_lease_count(re_lease_count);
        counts1.setIn_interest_count(multiply);
        counts1.setSer_charge(fwf);
        counts1.setCo_hirerent_count(zong);

        HashMap<String,Object> map=new HashMap<>();
        map.put("spList",spList);
        map.put("counts1",counts1);
        return map;
    }
}
